package com.itheima.organization.controller;

import com.github.pagehelper.Page;
import com.itheima.aom.pojo.AomOrder;
import com.itheima.aom.pojo.QueryPageBean;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.organization.service.AomOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/aomOrder")
public class AomOrderController {

    @Autowired
    private AomOrderService aomOrderService;

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Long id){
        AomOrder byId = null;
        try {
            byId = aomOrderService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, StatusCode.OK, "根据id查询农场订单失败");
        }
        return new Result(true, StatusCode.OK, "根据id查询农场订单成功", byId);
    }

    /**
     * 添加订单
     * @param aomOrder
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody AomOrder aomOrder){
        try {
            aomOrderService.add(aomOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "新增订单失败");
        }
        return new Result(true, StatusCode.OK, "新增订单成功");
    }

    /**
     * 修改订单
     * @param aomOrder
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody AomOrder aomOrder){
        try {
            aomOrderService.update(aomOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新订单失败");
        }
        return new Result(true, StatusCode.OK, "更新订单成功");
    }

    /**
     * 分页查询订单
     * @param serchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findPage/{page}/{size}")
    public Result findPage(@RequestParam Map<String, Object> serchMap, @PathVariable Integer page, @PathVariable Integer size){
        Page<AomOrder> aomOrderPage = null;
        PageResult<AomOrder> aomOrderPageResult;
        try {
            aomOrderPage = aomOrderService.findPage(serchMap, page, size);
            aomOrderPageResult = new PageResult<>(aomOrderPage.getTotal(), aomOrderPage.getResult());
            return new Result(true, StatusCode.OK, "分页查询成功", aomOrderPageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, StatusCode.ERROR, "分页查询失败");
        }
    }

}
